package com.IO.JieYaSuoLiu;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class A03 {
    public static void main(String[] args) throws IOException {
        /*
        * 压缩文件夹
        * */
        // 1.创建file对象表示要压缩的文件夹
        File src = new File("D:\\TestFile\\zip\\test2");
        // 2.创建file对象表示压缩包的路径
        // 获取文件的父路径
        String srcParent = src.getParent();
        File dest = new File(srcParent + ".zip");
        // 创建压缩流关联压缩包
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
        
        // 释放资源
        zos.close();
    }
    
    public static void zipFile(File src,ZipOutputStream zos,String name) throws IOException {
        File[] files = src.listFiles();
        for (File file : files) {
            if(file.isFile()){
                ZipEntry entry = new ZipEntry(name + "\\" + file.getName());
                zos.putNextEntry(entry);
                FileInputStream fis = new FileInputStream(file);
                int b;
                while ((b = fis.read()) != -1) {
                    zos.write(b);
                }
                fis.close();
                zos.closeEntry();
            } else {
                zipFile(file,zos,name + "\\" +file.getName());
            }
        }
    }
}
